package com.fyy.java8.strwam;

import com.fyy.java8.methodRef.User;
import com.fyy.java8.methodRef.UserData;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @Author fyy
 * @Description Stream API实例化
 * @Date 2021/2/26 9:49
 */
public class StreamNewTest {

    /** 通过集合创建Stream */
    public static void create1(){
        //获取一个集合
        List<User> users = UserData.getUsersData();
        //default Stream<E> stream() 返回一个顺序流，顺序执行中间链
        Stream<User> stream = users.stream();
        //default Stream<E> parallelStream() 返回一个并行流，类似于线程，并行执行中间链
        Stream<User> parallelStream = users.parallelStream();
    }

    /** 通过数组创建Stream */
    public static void create2(){
        //获取一个数组,静态初始化
        int[] arr = new int[]{1,2,3,4,5,6};
        //调用的是Arrays类的static <T> Stream<T> stream(<T>[] array)
        IntStream s1 = Arrays.stream(arr);
        //自定义数组
        User[] arr2 = new User[]{new User("李四","28"),new User("赵六","28")};
        Stream<User> s2 = Arrays.stream(arr2);
    }

    /** 通过Stream.of()创建Stream */
    public static void create3(){
        //不是数组形式，以包装类的形式
        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6);
    }

    /** 创建Stream无限流 */
    public static void create4(){
        //迭代 static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f)
        Stream.iterate(0,t -> t+2).limit(10).forEach(System.out::println);
        //生成 static<T> Stream<T> generate(Supplier<T> s)
        Stream.generate(()->2).limit(10).forEach(System.out::println);
    }

    public static void main(String[] args) {
//        create4();
        Random r = new Random(100);
        for (int i = 0; i < 10; i++) {
            System.out.println(r.nextInt(1000));
        }
    }

}
